﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace SHomeWorkshop.LunarPresentation.Adorners
{
    public class BaseTextCommentAdorner : Adorner
    {
        /// <summary>
        /// 用在文本块备注文本显示时的装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入文本块内部的备注TextBlock。</param>
        public BaseTextCommentAdorner(UIElement adornedElement)
            : base(adornedElement)
        {
            commentTextBlock = adornedElement as TextBlock;
            this.Opacity = 0.75;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
        }

        private TextBlock commentTextBlock;

        protected override void OnRender(DrawingContext drawingContext)
        {
            //Rect rect = new Rect(this.AdornedElement.RenderSize);

            Brush renderBrush = commentTextBlock.Foreground;
            Pen renderPen = new Pen(commentTextBlock.Foreground, 1);

            double fontSize = commentTextBlock.FontSize;

            double ptaY = commentTextBlock.ActualHeight / 2;

            Rect newRect = new Rect(-fontSize * 0.8, ptaY - fontSize / 2, fontSize * 0.8, fontSize);

            double width = newRect.Width;
            double height = newRect.Height;
            double halfHeight = height / 2;
            int quarterHeight = (int)height / 4;


            Point pta = new Point(newRect.Right, newRect.Bottom - halfHeight);
            Point ptb = new Point(newRect.BottomRight.X - halfHeight, newRect.Bottom);
            Point ptc = new Point(newRect.TopRight.X - halfHeight, pta.Y + quarterHeight);
            Point ptd = new Point(newRect.Left, ptc.Y);
            Point pte = new Point(ptd.X, pta.Y - quarterHeight + 1);
            Point ptf = new Point(ptb.X, pte.Y);
            Point ptg = new Point(ptb.X, newRect.Top);

            PathGeometry pathGeometry = new PathGeometry();
            PathFigure pf = new PathFigure() { StartPoint = pta };
            PolyLineSegment pls = new PolyLineSegment(
                new List<Point>()
                {
                    pta,ptb,ptc,ptd,pte,ptf,ptg,pta,
                }, true);
            pf.Segments.Add(pls);
            pathGeometry.Figures.Add(pf);

            drawingContext.DrawGeometry(renderBrush, renderPen, pathGeometry);

            //drawingContext.DrawLine(renderPen, pta, ptb);
            //drawingContext.DrawLine(renderPen, ptb, ptc);
            //drawingContext.DrawLine(renderPen, ptc, ptd);
            //drawingContext.DrawLine(renderPen, ptd, pte);
            //drawingContext.DrawLine(renderPen, pte, ptf);
            //drawingContext.DrawLine(renderPen, ptf, ptg);
            //drawingContext.DrawLine(renderPen, ptg, pta);

            //drawingContext.DrawRectangle(masterBaseText.ExForeground, renderPen, newRect);
        }
    }
}